﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace API.Models
{
    public interface ICommonDbClass
    {
        Guid GUID_RECORD { get; set; }

        DateTime? DATECREATE { get; set; }
        DateTime? DATEUPDATE { get; set; }

        Guid? USERCREATE_GUID { get; set; }
        Guid? USERUPDATE_GUID { get; set; }
    }

    public interface IDirectoryDbClass
    {
        Guid COMPANY_GUID { get; set; }
    }

    public interface IAdminDbClass { }

    public interface ICommonDirectoryDbClass : ICommonDbClass, IDirectoryDbClass { }
    public interface ICommonAdminDbClass : ICommonDbClass, IAdminDbClass { }

    public class CommonDbClass
    {
        [Key]
        public Guid GUID_RECORD { get; set; }

        public DateTime? DATECREATE { get; set; }
        public DateTime? DATEUPDATE { get; set; }
        public Guid? USERCREATE_GUID { get; set; }
        public Guid? USERUPDATE_GUID { get; set; }
    }

    public class EXT_ATTACHMENTS : CommonDbClass, ICommonDbClass
    {
        public Guid ITEM_GUID { get; set; }
        public string LINK { get; set; }
    }

    public class SEC_COMPANIES_ACCESS : CommonDbClass, ICommonDbClass
    {
        public Guid COMPANY_GUID { get; set; }
        public Guid ALLOW_TO_GUID { get; set; }
        public string SECURITY_CODE { get; set; }
        public bool IS_ALLOWED { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }

        [ForeignKey("ALLOW_TO_GUID")]
        public WH_COMPANIES ALLOW_COMPANY { get; set; }
    }

    public class SEC_COMPANIES_ACCESS_REQUESTS : CommonDbClass, ICommonDbClass
    {
        public Guid COMPANY_GUID { get; set; }
        public Guid? ALLOW_TO_GUID { get; set; }
        public Guid USER_GUID { get; set; }
        public Guid? ACTIVATION_USER_GUID { get; set; }
        public Guid? CONFIRMED_USER_GUID { get; set; }
        public string SECURITY_CODE_HASH { get; set; }
        public bool IS_ALLOWED { get; set; }
        public DateTime CREATE_DATE { get; set; }
        public DateTime? ACTIVATION_DATE { get; set; }
        public DateTime? CONFIRMED_DATE { get; set; }
        public DateTime EXPIRATION_DATE { get; set; }
        public Guid? ACCESS_GUID { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }
    }

    public class SEC_PERMISSIONS : CommonDbClass, ICommonDbClass
    {
        public string PERMISSION_NAME { get; set; }
        public string DESCRIPTION { get; set; }
    }

    public class SEC_ROLES : CommonDbClass, ICommonDbClass
    {
        public string ROLE_NAME { get; set; }
        public string DESCRIPTION { get; set; }
    }

    public class SEC_ROLE_PERMISSIONS : CommonDbClass, ICommonDbClass
    {
        public Guid ROLE_GUID { get; set; }
        public Guid PERMISSION_GUID { get; set; }
    }

    public class SEC_SESSIONS : CommonDbClass, ICommonDbClass
    {
        public Guid USER_GUID { get; set; }
        public Guid COMPANY_GUID { get; set; }
        public DateTime LOGON_DATETIME { get; set; }
        public DateTime? LOGOFF_DATETIME { get; set; }
        public string APIKEY { get; set; }
        public string IP { get; set; }

        [ForeignKey("USER_GUID")]
        public SEC_USERS USER { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }
    }

    public class SEC_USER_COMPANIES : CommonDbClass, ICommonDirectoryDbClass
    {
        public Guid USER_GUID { get; set; }

        [Column("ALLOW_TO_GUID")]
        public Guid COMPANY_GUID { get; set; }
        public string SECURITY_CODE { get; set; }
        public bool IS_ALLOWED { get;set;}

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }

        [ForeignKey("USER_GUID")]
        public SEC_USERS USER { get; set; }
    }


    public class V_SEC_USER_COMPANIES : CommonDbClass, ICommonDbClass
    {
        public Guid USER_GUID { get; set; }
        public Guid COMPANY_GUID { get; set; }
        public bool IS_OPERATOR { get; set; }

        //[ForeignKey("USER_GUID")]
        //public SEC_USERS USER { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }
    }

    public class SEC_USERS : CommonDbClass, ICommonDbClass
    {
        public string FIRSTNAME { get; set; }
        public string LASTNAME { get; set; }
        public string MIDNAME { get; set; }
        public string PASSWORD_HASH { get; set; }

        [NotMapped]
        public string PASSWORD { get; set; }


        [ForeignKey("USER_GUID")]
        public ICollection<V_SEC_USER_COMPANIES> COMPANIES { get; set; }

        //[ForeignKey("USER_GUID")]
        //public ICollection<SEC_USER_COMPANIES> ALLOW_COMPANIES { get; set; }

        [EmailAddress]
        public string EMAIL { get; set; }
        public int TIMEZONE { get; set; }

        [NotMapped]
        public IEnumerable<SEC_ROLES> ALLOW_ROLES { get; set; }
    }

    public class SEC_USERS_ROLES : CommonDbClass, ICommonDbClass
    {
        public Guid USER_GUID { get; set; }
        public Guid ROLE_GUID { get; set; }
    }

    public class WH_TAXES : CommonDbClass, ICommonDbClass
    {
        public string TAX_NAME { get; set; }
    }

    public class WH_COMPANIES : CommonDbClass, ICommonDbClass
    {
        public string COMPANY_NAME { get; set; }
        public string EXT_KEY { get; set; }

        [Column("OWNER")]
        public Guid? OWNER_GUID { get; set; }
        public string NOTE { get; set; }

        [ForeignKey("OWNER_GUID")]
        public WH_COMPANIES OWNER { get; set; }

        //[ForeignKey("COMPANY_GUID")]
        //public ICollection<WH_LOCATIONS> LOCATIONS { get; set; }
    }

    public class WH_INVOICES : CommonDbClass, ICommonDbClass
    {
        public short VERSION { get; set; }
        public Guid COMPANY_GUID { get; set; }
        public Guid? COMPANY_MAPPED_GUID { get; set; }
        public string DOC_ID { get; set; }
        public string DOC_NUMBER { get; set; }
        public DateTime DOC_DATE { get; set; }

        public Guid? CUSTOMER_GUID { get; set; }
        public Guid? CUSTOMER_MAPPED_GUID { get; set; }
        public Guid? LOCATION_GUID { get; set; }
        public Guid? LOCATION_MAPPED_GUID { get; set; }

        public string CUSTOMER_DOC_ID { get; set; }
        public short LINECOUNT { get; set; }
        public decimal TOTAL_AMOUNT { get; set; }
        public decimal TAX_AMOUNT { get; set; }
        public string NOTE { get; set; }

        [ForeignKey("INVOICE_GUID")]
        public ICollection<WH_INVOICE_LINES> LINES { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; } // с ключевым словом virtual ничего не работает :(

        [ForeignKey("COMPANY_MAPPED_GUID")]
        public WH_COMPANIES COMPANY_MAPPED { get; set; }

        [ForeignKey("CUSTOMER_GUID")]
        public WH_COMPANIES CUSTOMER { get; set; }

        [ForeignKey("CUSTOMER_MAPPED_GUID")]
        public WH_COMPANIES CUSTOMER_MAPPED { get; set; }

        [ForeignKey("LOCATION_GUID")]
        public WH_LOCATIONS LOCATION { get; set; }

        [ForeignKey("LOCATION_MAPPED_GUID")]
        public WH_LOCATIONS LOCATION_MAPPED { get; set; }
    }

    public class WH_INVOICE_LINES : CommonDbClass, ICommonDbClass
    {
        public Guid INVOICE_GUID { get; set; }
        public Guid? ITEM_GUID { get; set; }
        public Guid? ITEM_MAPPED_GUID { get; set; }
        public decimal PRICE { get; set; }
        public decimal QUANTITY { get; set; }
        public double TOTAL { get; set; }
        public decimal TAX_TOTAL { get; set; }
        public Int16 ORDER_NUM { get; set; } 

        [ForeignKey("ITEM_GUID")]
        public WH_ITEMS ITEM { get; set; }

        [ForeignKey("ITEM_MAPPED_GUID")]
        public WH_ITEMS ITEM_MAPPED { get; set; }

        //[ForeignKey("INVOICE_LINE_GUID")]
        //public ICollection<WH_INVOICE_LINES_TAXES> TAXES { get; set; }
    }

    public class WH_INVOICE_LINES_TAXES : CommonDbClass, ICommonDbClass
    {
        public Guid INVOICE_LINE_GUID { get; set; }
        public Guid TAX_GUID { get; set; }
        public decimal RATE { get; set; }
        public decimal AMOUNT { get; set; }

        [ForeignKey("TAX_GUID")]
        public WH_TAXES TAX { get; set; }
    }

    public class WH_ITEMS : CommonDbClass, ICommonDirectoryDbClass
    {
        public Guid COMPANY_GUID { get; set; }
        public string ITEM_NAME { get; set; }
        public string EXT_KEY { get; set; }
        public string ARTICLE { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }
    }

    public class WH_ITEMS_CODES : CommonDbClass, ICommonDbClass
    {
        public Guid ITEM_GUID { get; set; }
        public string ITEM_CODE { get; set; }
    }

    public class WH_LOCATIONS : CommonDbClass, ICommonDirectoryDbClass
    {
        public Guid COMPANY_GUID { get; set; }
        public string LOCATION_NAME { get; set; }
        public string EXT_KEY { get; set; }
        public string COUNTRY { get; set; }
        public string POSTAL_CODE { get; set; }
        public string CITY { get; set; }
        public string STREET { get; set; }
        public string BUILDING_NUMBER { get; set; }
        public string NOTE { get; set; }
        public bool IS_DEFAULT { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }
    }

    public class COMPANIES_MAPPING : CommonDbClass, ICommonDbClass
    {
        public Guid COMPANY_GUID { get; set; }
        public Guid SUPPLIER_GUID { get; set; }
    }

    public class LOCATIONS_MAPPING : CommonDbClass, ICommonDbClass
    {
        public Guid LOCATION_GUID { get; set; }
        public Guid SUPPLIER_LOCATION_GUID { get; set; }
    }

    public class ITEMS_MAPPING : CommonDbClass, ICommonDbClass
    {
        public Guid ITEM_GUID { get; set; }
        public Guid COMPANY_GUID { get; set; }

        public WH_ITEMS ITEM { get; set; }
    }


    public class V_OutgoingInvoices : CommonDbClass, ICommonDbClass, IDirectoryDbClass
    {
        public short VERSION { get; set; }
        public string DOC_ID { get; set; }
        public string DOC_NUMBER { get; set; }
        public DateTime DOC_DATE { get; set; }
        
        public string CUSTOMER_DOC_ID { get; set; }

        public Guid COMPANY_GUID { get; set; }
        public Guid CUSTOMER_GUID { get; set; }
        public Guid? LOCATION_GUID { get; set; }

        public short LINECOUNT { get; set; }
        public decimal TOTAL_AMOUNT { get; set; }
        public decimal TAX_AMOUNT { get; set; }
        public string NOTE { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }

        [ForeignKey("CUSTOMER_GUID")]
        public WH_COMPANIES CUSTOMER { get; set; }

        [ForeignKey("LOCATION_GUID")]
        public WH_LOCATIONS LOCATION { get; set; }

        [ForeignKey("INVOICE_GUID")]
        public ICollection<V_OutgoingInvoiceLines> LINES { get; set; }

        //public DateTime DATECREATE { get; set; }
        //public DateTime DATEUPDATE { get; set; }
    }

    public class V_IncomingInvoices : CommonDbClass, ICommonDbClass, IDirectoryDbClass
    {
        public short VERSION { get; set; }
        public string DOC_ID { get; set; }
        public string DOC_NUMBER { get; set; }
        public DateTime DOC_DATE { get; set; }

        public Guid COMPANY_GUID { get; set; }
        public Guid CUSTOMER_GUID { get; set; }
        public Guid? LOCATION_GUID { get; set; }

        public short LINECOUNT { get; set; }
        public decimal TOTAL_AMOUNT { get; set; }
        public decimal TAX_AMOUNT { get; set; }
        public string NOTE { get; set; }

        [ForeignKey("COMPANY_GUID")]
        public WH_COMPANIES COMPANY { get; set; }

        [ForeignKey("CUSTOMER_GUID")]
        public WH_COMPANIES CUSTOMER { get; set; }

        [ForeignKey("LOCATION_GUID")]
        public WH_LOCATIONS LOCATION { get; set; }

        [ForeignKey("INVOICE_GUID")]
        public ICollection<V_IncomingInvoiceLines> LINES { get; set; }

        //public DateTime DATECREATE { get; set; }
        //public DateTime DATEUPDATE { get; set; }
    }

    public class V_IncomingInvoiceLines : CommonDbClass, ICommonDbClass
    {
        public Guid INVOICE_GUID { get; set; }
        public Guid? ITEM_GUID { get; set; }
        public decimal PRICE { get; set; }
        public decimal QUANTITY { get; set; }
        public decimal TOTAL { get; set; }
        public decimal TAX_TOTAL { get; set; }
        public Int16 ORDER_NUM { get; set; } 

        [ForeignKey("ITEM_GUID")]
        public WH_ITEMS ITEM { get; set; }

        [ForeignKey("INVOICE_LINE_GUID")]
        public ICollection<WH_INVOICE_LINES_TAXES> TAXES { get; set; }
    }

    public class V_OutgoingInvoiceLines : CommonDbClass, ICommonDbClass
    {
        public Guid INVOICE_GUID { get; set; }
        public Guid? ITEM_GUID { get; set; }
        public decimal PRICE { get; set; }
        public decimal QUANTITY { get; set; }
        public decimal TOTAL { get; set; }
        public decimal TAX_TOTAL { get; set; }
        public Int16 ORDER_NUM { get; set; } 

        [ForeignKey("ITEM_GUID")]
        public WH_ITEMS ITEM { get; set; }

        [ForeignKey("INVOICE_LINE_GUID")]
        public ICollection<WH_INVOICE_LINES_TAXES> TAXES { get; set; }
    }

}